

/** INCLUDES *******************************************************/

#include "Compiler.h"
#include "GenericTypeDefs.h"
#include "HardwareProfile.h"
#include "usb_config.h"
#include "USB/usb_device.h"
#include "USB/usb.h"
#include "USB/usb_function_generic.h"
#include "chunk_buffer.h"

#include "user.h"


/** V A R I A B L E S ********************************************************/
#pragma udata

BYTE counter;

#pragma udata USB_VARS
DATA_PACKET INPacket;
DATA_PACKET OUTPacket;
DATA_KOMBI_PACKET LogPacket;
#pragma udata

USB_HANDLE USBGenericOutHandle = 0;
USB_HANDLE USBGenericInHandle = 0;

// Timer0 - 1 second interval setup.
// Fosc/4 = 12MHz
// Use /256 prescalar, this brings counter freq down to 46,875 Hz
// Timer0 should = 65536 - 46875 = 18661 or 0x48E5
#define TIMER0L_VAL         0xE5
#define TIMER0H_VAL         0x48

/** P R I V A T E  P R O T O T Y P E S ***************************************/


void setStateBits(int value);
void setServiceBits(int value);

void Show_USB_State(void);
void ServiceRequests(void);


/** D E C L A R A T I O N S **************************************************/
#pragma code
void UserInit(void)
{    
    #if defined(__18CXX)
    /* Init Timer0 for data logging interval (every 1 second) */
    T0CON = 0b10010111;
    /* Timer0 is already enabled by default */
    #elif defined(__C30__)
    #endif

    TRISD = 0x00; /* configure all PORTD pins for output */
    PORTD = 0x00; /* default LED configuration is off */

	mInitButton1();
	//setupTimer();
	TMR0H=0b11111100; 
	TMR0L=0b11111111;
	TMR1H=0;
	TMR1L=0;
	enableTimer();
	enableTimeCounter();
	clearTimerInterrupt();
	enableTimerInterrupt();
	selectADCChannel0();
	enableADC();
	setupADC();
	setADCInterruptPrioHigh();
	clearADCInterrupt();
	enableADCInterrupt();
	enableGlobalInterrupt();


}//end UserInit


/******************************************************************************
 * Function:        void ProcessIO(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This function is a place holder for other user routines.
 *                  It is a mixture of both USB and non-USB tasks.
 *
 * Note:            None
 *****************************************************************************/
void ProcessIO(void)
{   
    
    Show_USB_State();

    // User Application USB tasks
    if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) 
		return;

    ServiceRequests();

}//end ProcessIO


/******************************************************************************
 * Function:        void ServiceRequests(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    USB traffic can be generated
 *
 * Overview:        This function takes in the commands from the PC from the
 *                  application and executes the commands requested
 *
 * Note:            None
 *****************************************************************************/
void ServiceRequests(void)
{
    BYTE i;
	
    //Check to see if data has arrived
    if(!USBHandleBusy(USBGenericOutHandle))
    {
		INPacket.length = OUTPacket.length;
       
        if(!USBHandleBusy(USBGenericInHandle))
		{
			chunk_buffer_getdata (&LogPacket);
			//PORTD = LogPacket.length;
			USBGenericInHandle = USBGenWrite(USBGEN_EP_NUM,(BYTE *)&LogPacket/*INPacket*/,
				LogPacket.length * sizeof(CHUNK) + sizeof(WORD)/*OUTPacket.length+1*/);
		}

        //Re-arm the OUT endpoint for the next packet
       USBGenericOutHandle = USBGenRead(USBGEN_EP_NUM,(BYTE*)&OUTPacket,USBGEN_EP_SIZE);
    }//end if

}//end ServiceRequests


void setStateBits(int value)
  {
	if (button1 != 0)
		PORTD = value | (0b00011111 & LATD);
  }

void setServiceBits(int value)
  {
  //  PORTD = value | (0b00011111 & LATD);
  //    PORTD = value << 2; // | (0b11100000 & LATD);
  //	  PORTD = 0b0101;
  }

void Show_USB_State(void)
  {
    static int configured = 1; 

    if(USBSuspendControl == 1)
      {
        configured = 0;
        setStateBits(0b00000000);
        return;
      }

    switch (USBDeviceState)
      {
        case POWERED_STATE:
                configured = 0;
                setStateBits(0b10000000); 
                break;
        case DETACHED_STATE:
                configured = 0;
                setStateBits(0b01000000); 
                break;
        case ATTACHED_STATE:	
                configured = 0;
                setStateBits(0b11000000); 
                break;
        case DEFAULT_STATE:		
                configured = 0;
                setStateBits(0b00100000); 
                break;
        case ADDRESS_STATE:		
                configured = 0;
                setStateBits(0b10100000); 
                break;
        case CONFIGURED_STATE:	
                if (configured) break;
                configured = 1;
                setStateBits(0b11100000); 
                break; 
      }
  }



